""" F(x) -> u*y """
from common import *
from pylab import *
import sys

default_data = 'sin(x)*u'

def setup(data = default_data):
    import wx
    ok = False
    error_msg = ''
    func = data
    while not ok:
        dlg = wx.TextEntryDialog(None, error_msg + 'f(x) = ', 'Configure Process', func)
        
        if dlg.ShowModal() == wx.ID_OK:
            func = dlg.GetValue()
            try:
                u = 1.0
                x = arange(1.0,2.0,0.1)
                y = eval(func)
                data = func
                ok = True
            except Exception, e:
                error_msg = str(e) + '\n'
        else:
            ok = True
        dlg.Destroy()
    return data

def pylab_F_x_yFunc(x_in, u_in, y, data = default_data):
    
    u = u_in()            

    x = x_in()
    if (type(x) == tuple):
        job_id = x[0]
        x = x[1]
        job = eval(data)
        y((job_id,job))
    else:
        y(eval(data))
        
    while True:
        
        alt = Alternation([{x_min:None, u_in:None}])                    
        (ret, msg) = alt.select()
        if (ret == x_in):
            # Compute
            x = msg
            if (type(x) == tuple):
                job_id = x[0]
                x = x[1]
                job = eval(data)
                y((job_id,job))
            else:
                y(eval(data))
            
        elif (ret == u_in):
            # Update u
            u = msg
            
        
